package com.nfc.lock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nfc.lock.common.result.Result;
import com.nfc.lock.dto.request.UserAddRequest;
import com.nfc.lock.dto.response.UserInfoVO;
import com.nfc.lock.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户Controller
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @Operation(summary = "分页查询用户列表")
    @GetMapping("/list")
    public Result<IPage<UserInfoVO>> getUserList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "真实姓名") @RequestParam(required = false) String realName,
            @Parameter(description = "角色类型") @RequestParam(required = false) Integer roleType,
            @Parameter(description = "区域ID") @RequestParam(required = false) Long regionId
    ) {
        return Result.success(userService.getUserList(pageNum, pageSize, realName, roleType, regionId));
    }

    @Operation(summary = "新增用户")
    @PostMapping("/add")
    public Result<Void> addUser(@Valid @RequestBody UserAddRequest request) {
        userService.addUser(request);
        return Result.success();
    }

    @Operation(summary = "编辑用户")
    @PutMapping("/edit/{userId}")
    public Result<Void> updateUser(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Valid @RequestBody UserAddRequest request
    ) {
        userService.updateUser(userId, request);
        return Result.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/delete/{userId}")
    public Result<Void> deleteUser(@Parameter(description = "用户ID") @PathVariable Long userId) {
        userService.deleteUser(userId);
        return Result.success();
    }

    @Operation(summary = "重置密码")
    @PutMapping("/resetPassword/{userId}")
    public Result<Void> resetPassword(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "新密码") @RequestParam String newPassword
    ) {
        userService.resetPassword(userId, newPassword);
        return Result.success();
    }
}
